package com.cskaoyan.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author stone
 * @date 2022/04/29 17:49
 */
@RestController
@RequestMapping("upload")
public class UploadController {

    @RequestMapping("file")
    public String uploadFile(MultipartFile myfile) throws IOException {
        //transferTo → 参数 MultipartFile转换为一个什么样的file
        String name = myfile.getName();  //请求参数名 → myfile
        String originalFilename = myfile.getOriginalFilename(); //上传时的文件名
        long size = myfile.getSize(); //文件大小
        String contentType = myfile.getContentType(); //正文类型 xxx/xxx
        //如果上传图片文件 → image/jpeg

        //myfile.transferTo(new File("D:\\tmp","dlrb.jpg"));
        myfile.transferTo(new File("D:\\tmp",originalFilename));
        return "ok";
    }
    @RequestMapping("files")
    public String uploadFiles(MultipartFile[] myfiles) throws IOException {
        for (MultipartFile myfile : myfiles) {
            //transferTo → 参数 MultipartFile转换为一个什么样的file
            String name = myfile.getName();  //请求参数名 → myfile
            String originalFilename = myfile.getOriginalFilename(); //上传时的文件名
            long size = myfile.getSize(); //文件大小
            String contentType = myfile.getContentType(); //正文类型 xxx/xxx
            //如果上传图片文件 → image/jpeg

            //myfile.transferTo(new File("D:\\tmp","dlrb.jpg"));
            myfile.transferTo(new File("D:\\tmp",originalFilename));
        }
        return "ok";
    }
}
